<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>SQL converter form</title>
    <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <script type="text/javascript">
      function toJava(obj) {
        var text = document.getElementById("text").value;
        var lines = text.split("\n");
        var start = "\"";
        var result = "";
        for(i = 0; i < lines.length; i++) {
          if(lines[i].trim().length > 0) {
            if(lines[i].indexOf(start) == -1 && lines[i].indexOf("\\n\" +") == -1 ) {
              result += start + lines[i] + "\\n\" +\n";
            } else {
              //already java string format
              result += lines[i] + "\n";
            }
          }
        }

        document.getElementById("text").value = result;
      }

      function toSql(obj) {
        var text = document.getElementById("text").value;
        var lines = text.split("\n");
        var start = "";
        var result = "";
        for(i = 0; i < lines.length; i++) {
          if(lines[i].trim().length > 0) {
            var index = lines[i].indexOf("\"");

            if(index == -1) {
              //check if it is already sql format
              rem2 = lines[i];
            } else {
              index = lines[i].indexOf("\""); //find first " char
              var rem = lines[i].substring(index + 1);
              var rem2 = rem.substring(0, rem.indexOf("\\n"));

              //if this was last no \n char is found check
              if(rem2.trim().length == 0)
              {
                rem2 = rem.substring(0, rem.indexOf("\""));
              }
            }
            result += rem2 + "\n";
          }
        }

        document.getElementById("text").value = result;
      }

      function toJavaPlusEquals(obj) {
        var text = document.getElementById("text").value;
        var lines = text.split("\n");
        var start = "sql += \"";
        var result = "";
        for(i = 0; i < lines.length; i++) {
          if(lines[i].trim().length > 0) {
            if(lines[i].indexOf("sql +=") == -1) {
              result += start + lines[i] + "\\n\";\n";
            } else {
              //already java string format
              result += lines[i] + "\n";
            }
          }
        }

        document.getElementById("text").value = result;
      }

      function toSqlPlusEquals(obj) {
        var text = document.getElementById("text").value;
        var lines = text.split("\n");
        var start = "";
        var result = "";
        for(i = 0; i < lines.length; i++) {
          if(lines[i].trim().length > 0) {
            var index = lines[i].indexOf("sql +=");

            if(index == -1) {
              //check if it is already sql format
              rem2 = lines[i];
            } else {
              index = lines[i].indexOf("\""); //find first " char
              var rem = lines[i].substring(index + 1);
              var rem2 = rem.substring(0, rem.indexOf("\\n"));

              //if this was last no \n char is found check
              if(rem2.trim().length == 0)
              {
                rem2 = rem.substring(0, rem.indexOf("\""));
              }
            }
            result += rem2 + "\n";
          }
        }

        document.getElementById("text").value = result;
      }
    </script>
  </head>
  <body>
    <h2>Convert SQL command to Java or the other way</h2>
    <textarea rows="25" cols="80" style="width: 100%" id="text"></textarea>
    <br/>
    <input type="button" onclick="javascript:toSql(this)" value="To SQL +"/>
    <input type="button" onclick="javascript:toJava(this)" value="To Java +"/>
    <br/><br/>
    <input type="button" onclick="javascript:toSqlPlusEquals(this)" value="To SQL +="/>
    <input type="button" onclick="javascript:toJavaPlusEquals(this)" value="To Java +="/>
    <br/><br/>
  </body>
</html>
